﻿@using MarkdownWeb.Models;

@{
    WikiContent content = ViewData["content"] as WikiContent;
    ViewBag.Title = content.Title;
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>编辑:@(content.Title)</h2>
<div class="container">
    <div class="row">
        <div class="col-md-6">
            <input type="text" class="form-control" id="title" style="width:100%" value="@content.Title" readonly="readonly" />
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <textarea class="form-control" rows="38" id="content" onkeyup="compile()">@content.Content</textarea>
        </div>
        <div class="col-md-6" id="result">            
        </div>
    </div>
    <div class="row">
        <div class="col-md-2">
            <button class="btn btn-primary" onclick="save()">保存</button>
        </div>
    </div> 
</div>

<script type="text/javascript">
    window.onload=function () {
        compile();
    };
    function compile() {
        var text = document.getElementById("content").value;
        var converter = new showdown.Converter();
        var html = converter.makeHtml(text);
        document.getElementById("result").innerHTML = html;
    }
    function save() {
        var text = document.getElementById("content").value;
        var t = document.getElementById("title").value;
        var postdata = {
            title: t,
            data: text
        };
        $.post("/Edit/Save", postdata, function (resp) {           
            if (resp.result == "ok") {
                alert("保存成功");
                var url = "/" + resp.title;
                window.location.href = url;              
            }
            else {
                alert(resp.result);               
            }
        }, "json");

    }
</script>

@section scripts{

    <script src="~/Scripts/showdown.min.js"></script>   
}
